/**
 *作者：yhb.
 *时间：2011-2-17 10:06:06
 *对象功能：这是一个用于权限的细粒度控制的权限控件。(基于rbac的权限控制。)
 */
package com.towker.frame.share.tag;

import javax.servlet.http.*;
import javax.servlet.jsp.JspTagException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.BodyTagSupport;

import com.towker.core.util.BeanUtils;
import com.towker.frame.share.rbac.res.PermissionShare;
import com.towker.frame.share.sys.user.UserShare;

@SuppressWarnings("serial")
public class PermissionTag extends BodyTagSupport {
	//资源id
	private String resId = "";
	//标签类型
	private String type="";
	//超链接地址
	private String href="";
	//标签id
	private String id="";
	private String extend="";
	public void setResId(String resId){this.resId = resId;}
	public void setType(String type){this.type = type;}
	public void setHref(String href){this.href = href;}
	public void setId(String id){this.id = id;}
	public void setExtend(String extend){this.extend = extend;}
	public int doStartTag() throws JspTagException{
		return EVAL_BODY_BUFFERED;
	}
	/**标签结束*/
	public int doEndTag() throws JspTagException{
		JspWriter writer = pageContext.getOut();
		try{
			if("a".equals(type)){
				if(isResIdPermission())writer.print("<a id='"+id+"' href='"+href+"' "+extend+">"+bodyContent.getString()+"</a>");
				else writer.print("<a id='"+id+"' href='"+href+"' disabled='disabled' "+extend+">"+bodyContent.getString()+"</a>");
			}
		}catch (Exception e){}
		return SKIP_BODY;
	}
	/**是否引用资源别名有权限*/
	private boolean isResIdPermission(){
		PermissionShare permissionHelper=(PermissionShare)BeanUtils.getBean(PermissionShare.class);
		HttpServletRequest request=(HttpServletRequest)pageContext.getRequest();
		return permissionHelper.isResIdPermission(UserShare.getCurrentUserId(request), UserShare.getCurrentSystemId(request), resId);
	}
}
